﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView.WordAction
{
    public partial class WagesApprovedWindow : ChildWindow
    {
        private readonly WordActionService.WordActionClient _client = new WordActionService.WordActionClient();
        
        private WordActionService.Word_WagesApproved entity = null;
        private int errorCount = 0;
        private int orginalYear = 0;
        private int orginalNO = 0;
        public WagesApprovedWindow(int id)
        {
            InitializeComponent();
            _client.SaveWagesApprovedCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(_client_SaveWagesApprovedCompleted);
            this.BindingValidationError += (sender, e) =>
            {
                if (e.Action == ValidationErrorEventAction.Added)
                {
                    errorCount++;
                }
                else if (e.Action == ValidationErrorEventAction.Removed)
                {
                    errorCount--;
                }
            };
            _client.GetTopNOAcoordingToYearCompleted += new EventHandler<WordActionService.GetTopNOAcoordingToYearCompletedEventArgs>(_client_GetTopNOAcoordingToYearCompleted);
            if (id > 0)
            {
                _client.GetWagesApprovedCompleted += (sender, e) =>
                {
                    entity = e.Result.FirstOrDefault();
                    orginalYear = entity.Year;
                    orginalNO = entity.NO;
                    DataBinding();
                };
                _client.GetWagesApprovedAsync("ID=" + id);
                
            }
            else
            {
                entity = new WordActionService.Word_WagesApproved() { Year = DateTime.Now.Year, CeateDate = DateTime.Now, EffectiveDate = DateTime.Now };
                _client.GetTopNOAcoordingToYearAsync(entity.Year, "init");
                
            }
        }

        void _client_SaveWagesApprovedCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("保存成功");
                this.DialogResult = true;
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("保存失败,可能原因:\n" + e.Error.Message);
            }
        }

        void _client_GetTopNOAcoordingToYearCompleted(object sender, WordActionService.GetTopNOAcoordingToYearCompletedEventArgs e)
        {
            string us = e.UserState.ToString();
            switch (us)
            {
                case "init":
                    entity.NO = e.Result + 1;
                    DataBinding();
                    break;
                case "Check":
                    if (entity.Year > e.Result)
                        _client.SaveWagesApprovedAsync(entity);
                    else
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("你所使用的文号已经被使用");
                    break;
            }
        }



        private void DataBinding()
        {
            var _entity = CommunitySystem.Model.Silverlight.EntityConvertor.Convert<CommunitySystem.Model.Silverlight.Word_WagesApproved>(entity);
            this.DataContext = _entity;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            Validate();
            if (errorCount > 0)
                return;
            var _entity = this.DataContext as CommunitySystem.Model.Silverlight.Word_WagesApproved;
            entity = CommunitySystem.Model.Silverlight.EntityConvertor.Convert<CommunitySystem.WordActionService.Word_WagesApproved>(_entity);
            if (entity.ID > 0 && entity.Year == orginalYear && entity.NO == orginalNO)
                _client.SaveWagesApprovedAsync(entity);
            else
            {
                _client.GetNOAcoordingToYearAsync(entity.Year, "Check");
            }
            //_client.SaveWagesApprovedAsync(entity);
            //this.DialogResult = true;
        }

        private void Validate()
        {
            System.Windows.Data.BindingExpression ex = tbName.GetBindingExpression(TextBox.TextProperty);
            ex.UpdateSource();
            ex = tbNO.GetBindingExpression(TextBox.TextProperty);
            ex.UpdateSource();
            ex = tbYear.GetBindingExpression(TextBox.TextProperty);
            ex.UpdateSource();
            ex = tbOD.GetBindingExpression(TextBox.TextProperty);
            ex.UpdateSource();
            ex = tbOP.GetBindingExpression(TextBox.TextProperty);
            ex.UpdateSource();
            ex = tbReason.GetBindingExpression(TextBox.TextProperty);
            ex.UpdateSource();
            ex = tbND.GetBindingExpression(TextBox.TextProperty);
            ex.UpdateSource();
            ex = dpED.GetBindingExpression(DatePicker.SelectedDateProperty);
            ex.UpdateSource();
            ex = dpCreateDate.GetBindingExpression(DatePicker.SelectedDateProperty);
            ex.UpdateSource();
            ex = tbAs.GetBindingExpression(TextBox.TextProperty);
            ex.UpdateSource();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void btnTemp_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}

